$(function(){
	
	//权限管理全选
	$(".item-title input:checkbox").change(function(){
		if($(this).is(":checked")){
			$(this).parent(".item-title")
			       .next().find("input:checkbox")
			       .prop("checked",this.checked);
		}else{
			$(this).parent(".item-title")
			       .next().find("input:checkbox")
			       .attr("checked",this.checked);
		}
		
	});
	
	$("#roleSumbit").click(function(){
	   var userSelect=new Array();
	   var proSelect=new Array();
	   var fileSelect=new Array();
	   var msgSelect=new Array();
	   
	   var roleName = $("#roleName").val();
	   
	   if(roleName=="" || roleName==null){
	   	    showTips("请输入角色名称！");
	   	    return false;
	   }
	   
	   $("input:checkbox[name=userSelect]:checked").each(function(){ 
	    	 userSelect.push($(this).val())
	   })
	   
	   $("input:checkbox[name=proSelect]:checked").each(function(){
		   proSelect.push($(this).val())
	   })
	   $("input:checkbox[name=fileSelect]:checked").each(function(){
		   fileSelect.push($(this).val())
	   })
	   $("input:checkbox[name=msgSelect]:checked").each(function(){
		   msgSelect.push($(this).val())
	   })
	   var securityArry = '';
	   if(userSelect != null && userSelect != '') {
		   securityArry+=userSelect+",";
	   }
	   if(proSelect != null && proSelect != '') {
		   securityArry+=proSelect+",";
	   }
	   if(fileSelect != null && fileSelect != '') {
		   securityArry+=fileSelect+",";
	   }
	   if(msgSelect != null && msgSelect != '') {
		   securityArry+=msgSelect+",";
	   }
	   console.log(securityArry);
	   var roleId = $("#roleId").val();
	   var url = '';
	   if(roleId == null || roleId == 0) {
		   url = '/role/addRole';
	   } else {
		   url = "/role/updateRole/"+ roleId;
	   }
	   
	  $.ajax({
			type:"post",
			url:url,
			data:{"name":roleName,"securityArry":securityArry},
			dataType:"json",
			success:function(data){
				if(data.status == true) {
					 if(roleId == null || roleId == 0) {
					 	console.log("新增角色成功");
					 } else {
						console.log("修改角色成功");
					 }
					 //location.reload();
					 window.location.href="../../user/list";
				} else {
					showTips("新增角色失败");
				}
			},
			error:function(data){
				showTips("新增角色失败，请重新添加");
			}
		});
	});
	
	/*信息提示框*/
	function showTips(str){
		var doc=document;
		var timer=null;
		
		//如果提示框已被创建，则只改变文本内容，否则创建提示框
		if(doc.querySelector(".tips")!==null){
			doc.querySelector(".tips span").innerHTML=str;
			doc.querySelector(".tips").style.display="block";
		}else{
			var body=doc.querySelector("body"),
				tips=doc.createElement("div"),
				span=doc.createElement("span");
			
			//设置提示框样式，并将节点插入到body中
			tips.style.cssText="position:fixed; top:0;z-index:9999; width:100%; height:100%; background-color:rgba(0,0,0,.5);color:#23a8f5;";
			span.style.cssText="max-width:60%;padding:30px 50px;border-radius:2px;background-color:#fff;position:relative;top:50%;transform:translateY(-50%);margin-left:auto;margin-right:auto;box-shadow:0 0 20px #000; display:inline-block; text-align:left;";
			span.innerHTML=str;
			tips.appendChild(span);
			tips.className="tips text-center";
			body.appendChild(tips);
			
			//点击时隐藏提示框
			tips.onclick=function(){
				clearTimeout(timer);
				this.style.display="none";
			};
		};
		
		timer=setTimeout(function(){
			doc.querySelector(".tips").style.display="none";
		},3000);
	
	};
})
